package com.quantumoctopus.magiceatball;

import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class QuizActivity extends Activity {

	private TextView questionText;
	private ImageView eBack;
	private Button yesButton;
	private Button noButton;
	private QuizManager qm;
	private Question q;
	private ArrayList<Button> buttonList = new ArrayList<Button>();
	public static final String PREFS_EATBALL = "EatBallPrefsFile";
	static SharedPreferences settings;
	SharedPreferences.Editor editor;
	private String coordinates;
	private ArrayList<Result> resultList;
	private boolean resultsNotFound = false;
	private Animation scale;
	private Animation rollin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.quiz_activity_layout);
		questionText = (TextView) findViewById(R.id.nameTextView);
		eBack = (ImageView) findViewById(R.id.eballBackImgView);
		rollin = AnimationUtils.loadAnimation(this, R.anim.rollin);
		scale = AnimationUtils.loadAnimation(this, R.anim.fadein);
		yesButton = (Button) findViewById(R.id.yesButton);
		yesButton.setVisibility(View.INVISIBLE);
		noButton = (Button) findViewById(R.id.noButton);
		buttonList.add(yesButton);
		buttonList.add(noButton);
		Intent intent = getIntent();
		coordinates = intent.getStringExtra("coordinates");
		// SharedPreferences settings = getPreferences(MODE_PRIVATE);
		settings = getSharedPreferences(PREFS_EATBALL, 0);

		qm = new QuizManager();
		qm.LoadPrefs(settings);
		q = qm.startQuiz(getResources().getText(R.string.questions).toString());
		buildActivity();

		eBack.startAnimation(rollin);
		rollin.setAnimationListener(new AnimationListener() {
			@Override
			public void onAnimationEnd(Animation animation) {
				yesButton.setVisibility(View.VISIBLE);
				yesButton.startAnimation(scale);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationStart(Animation animation) {
			}

		});
	}

	public void buildActivity() {
		questionText.setText(q.getText());
		for (int i = 0; i < q.getCount(); i++)
			buttonList.get(i).setText(q.getAnswer(i));
	}

	public void nextQuestion() {
		q = qm.nextQuestion();
		yesButton.startAnimation(scale);
		buildActivity();
	}

	public void getResults() {
		Animation spin = AnimationUtils.loadAnimation(this, R.anim.spin);
		yesButton.startAnimation(spin);
		yesButton.setClickable(false);
		noButton.setClickable(false);
		new GetResultTask().execute();

	}

	private class GetResultTask extends AsyncTask<String, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(String... params) {

			try {
				resultList = qm.getResultList(coordinates);

				qm.setPricecare(settings.getBoolean("pricecare", true));
				qm.setNewloc(settings.getBoolean("newloc", false));
				qm.setPrice(settings.getInt("price", 4));

				Result r = qm.filterResultList(resultList);

				Intent intent = new Intent(QuizActivity.this,
						ResultActivity.class);
				intent.putExtra("name", r.getName());
				intent.putExtra("display_address", r.getDisplay_address());
				intent.putExtra("phone", r.getPhone());
				intent.putExtra("url", r.getUrl());
				intent.putExtra("id", r.getId());
				intent.putExtra("price", r.getPriceString());
				intent.putExtra("rating", Float.parseFloat(r.getRating()));
				startActivity(intent);
				finish();
			} catch (Exception e) {
				resultsNotFound = true;
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void result) {

			yesButton.clearAnimation();
			yesButton.setClickable(true);
			noButton.setClickable(true);
			if (resultsNotFound) {
				Context context = getApplicationContext();
				CharSequence text = "Reply hazy, try again";
				int duration = Toast.LENGTH_LONG;

				Toast toast = Toast.makeText(context, text, duration);
				toast.show();
				finish();
			}
			super.onPostExecute(result);
		}
	}

	@Override
	public void finish() {
		super.finish();
	}

	public void click(View view) {
		Button b = (Button) view;
		String buttonText = b.getText().toString();
		// rotate360(b);
		if (qm.answerQuestion(buttonText))
			nextQuestion();
		else
			getResults();

	}

	public void rotate360(View view) {
		Animation rotate = new RotateAnimation(0.0f, 360.0f,
				view.getWidth() / 2, view.getHeight() / 2);
		rotate.setDuration(500);
		view.startAnimation(rotate);

	}

}
